package com.yanqu.road.web.mgr;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.entity.web.RecommendServerReturnParam;
import com.yanqu.road.logic.redis.RedisServerRegisterPlayerNumHelper;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.web.WebServer;
import redis.clients.jedis.Tuple;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author wpj
 * @Date 2020/11/14
 */
public class RegisterPlayerNumMgr extends TempMgr {

    private static Map<Long, Integer> serverRegisterNumMap = new ConcurrentHashMap<>();

    public static Map<Long, Integer> getServerRegisterNumMap() {
        return serverRegisterNumMap;
    }

    public static void setServerRegisterNumMap(Map<Long, Integer> serverRegisterNumMap) {
        RegisterPlayerNumMgr.serverRegisterNumMap = serverRegisterNumMap;
    }

    /**
     * 获取注册人数
     * @param serverId
     * @return
     */
    public static int getRegisterNumByServerId(long serverId) {
        if (serverRegisterNumMap.containsKey(serverId)) {
            return serverRegisterNumMap.get(serverId);
        } else {
            return 0;
        }
    }

    @Override
    public boolean reloadData() throws Exception {
        loadRegisterNum();
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static void loadRegisterNum() {
        if (!WebServer.getInstance().isUserCenter()) {
            // 非用户中心不需要加载 TODO return;
            return;
        }
        try {
            Set<Tuple> tupleSet = RedisServerRegisterPlayerNumHelper.getRegisterNumSet();
            Iterator<Tuple> iterator = tupleSet.iterator();
            while(iterator.hasNext()) {
                Tuple tuple = iterator.next();
                long serverId = Long.valueOf(tuple.getElement());
                int num = (int) tuple.getScore();
                serverRegisterNumMap.put(serverId, num);
            }

            getLogger().info("register count:{}", JSONObject.toJSONString(serverRegisterNumMap));
        } catch (Exception ex) {
            getLogger().error(ex);
        }
    }

    /**
     * 获取一个推荐区服
     * @return
     */
    public static RecommendServerReturnParam getRecommendServerInfo(List<ServerInfo> serverInfoList) {
        if (serverInfoList == null || serverInfoList.size() == 0) {
            return null;
        }


        List<ServerInfo> recommendList = new ArrayList<>();
        List<Long> serverIdList = new ArrayList<>();
        for (ServerInfo serverInfo : serverInfoList) {
            if (!serverInfo.isOpenSplitFlow()) {
                continue;
            }

            // 期待状态的不能加入到推荐区服
            if (ServerListMgr.isComingSoon(serverInfo)) {
                continue;
            }

            // 预创角不加入推荐区服
            if(ServerListMgr.isPreRole(serverInfo.getServerId())){
                continue;
            }

            if (getRegisterNumByServerId(serverInfo.getServerId()) < serverInfo.getRegisterLimit()) {
                recommendList.add(serverInfo);
                serverIdList.add(serverInfo.getServerId());
            }
        }

        if (recommendList.size() == 0) {
            return null;
        }

        int randomIndex = new Random().nextInt(recommendList.size());
        ServerInfo serverInfo = recommendList.get(randomIndex);
        return new RecommendServerReturnParam(serverIdList, serverInfo);
    }
}

